<template>
  <a-modal :width="modalWidth"
           :title="title"
           :visible.sync="visible"
           @cancel="handleCancel"
           :footer="disableSubmit ? null : undefined">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
          <a-form-item label="表单名称" :labelCol="labelCol" :wrapperCol="wrapperCol" class="mt15">
              <a-input placeholder="请输入表单名称" v-decorator.trim="[ 'name', validatorRules.name]" :disabled="disableSubmit"/>
          </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button key="back"  @click="handleCancel">
        取消
      </a-button>
      <a-button key="submit" type="primary" :loading="confirmLoading"  @click="handleSubmit">
        提交
      </a-button>
    </template>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import moment from 'moment'
  import {copyBpmForm} from '@/api/api'

  export default {
    name: "FormModal",
    components: {
    },
    data () {
      return {
        modalWidth:800,
        id:"",
        localFormType:"",
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
          validatorRules: {
              name: {rules: [{required: true, message: '请输入表单名称!'}]},
          },
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        headers:{},
        confirmLoading: false,
        form:this.$form.createForm(this),
      }
    },
    methods: {
      copy (record) {
        let that = this;
        that.form.resetFields();
        that.model.id = record.id;
        that.model.name = record.name+"_copy";
        that.visible = true;
        that.$nextTick(() => {
          that.form.setFieldsValue(pick(this.model,'name'));
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.disableSubmit = false;
      },
      moment,
      handleSubmit () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            formData.id=this.model.id;
            copyBpmForm(formData).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>